package com.example.MatchGame;

import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.wonderfulradish.R;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MatchGame extends AppCompatActivity implements View.OnClickListener{
    private ProgressBar progressBar;
    private Button  leaveGame,pushGame,pointButton,testButton;
    private ImageView part1,part2,part3,part4,part5,part6,part7,part8,part9,part10,part11,part12,part13,part14,part15,part16;
    public ImageView[] luobo;
    private TextView gameTime,scoreText;
    public int two_1=0,two_2=0,two_3=0,two_4=0,two_5=0,two_6=0,two_7=0,two_8=0,two_9=0,two_10=0,two_11=0,two_12=0,two_13=0,two_14=0,two_15=0,two_16=0;
    public int res1,res2,res3,res4,res5,res6,res7,res8,res9,res10,res11,res12,res13,res14,res15,res16;
    private int recLen = 20;
    public int flag=1;
    public int result=0;
    public final int GREEN=0;
    public final int ORANGE=1;
    public final int PURPLE=2;
    Timer timer = new Timer();
    private int point=1;
    private int progress;

    private Animation pointAnimation;
    private int color ;

    private MediaPlayer mp;
    private int score=0;
    private ImageView selectColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_game);

        progressBar=(ProgressBar) findViewById(R.id.match_progressBar);
        leaveGame=(Button) findViewById(R.id.leave_game);
        leaveGame.setOnClickListener(this);
        pushGame=(Button) findViewById(R.id.push_time);
        pushGame.setOnClickListener(this);

        gameTime=(TextView) findViewById(R.id.game_time);
        timer.schedule(task, 1000, 1000);       // timeTask

        pointButton=(Button) findViewById(R.id.point_button);
        pointButton.setOnClickListener(this);

        scoreText=(TextView) findViewById(R.id.match_score_text);

        //选中的方式图片
        selectColor=(ImageView) findViewById(R.id.match_select_color);

        //游戏二算法部分
            //获取各个萝卜的ID
        luobo=new ImageView[16];
        part1=luobo[0]=(ImageView)findViewById(R.id.partt1);
        part2=luobo[1]=(ImageView)findViewById(R.id.partt2);
        part3=luobo[2]=(ImageView)findViewById(R.id.partt3);
        part4=luobo[3]=(ImageView)findViewById(R.id.partt4);
        part5=luobo[4]=(ImageView)findViewById(R.id.partt5);
        part6=luobo[5]=(ImageView)findViewById(R.id.partt6);
        part7=luobo[6]=(ImageView)findViewById(R.id.partt7);
        part8=luobo[7]=(ImageView)findViewById(R.id.partt8);
        part9=luobo[8]=(ImageView)findViewById(R.id.partt9);
        part10=luobo[9]=(ImageView)findViewById(R.id.partt10);
        part11=luobo[10]=(ImageView)findViewById(R.id.partt11);
        part12=luobo[11]=(ImageView)findViewById(R.id.partt12);
        part13=luobo[12]=(ImageView)findViewById(R.id.partt13);
        part14=luobo[13]=(ImageView)findViewById(R.id.partt14);
        part15=luobo[14]=(ImageView)findViewById(R.id.partt15);
        part16=luobo[15]=(ImageView)findViewById(R.id.partt16);
        luobo[0].setOnClickListener(this);
        luobo[1].setOnClickListener(this);
        luobo[2].setOnClickListener(this);
        luobo[3].setOnClickListener(this);
        luobo[4].setOnClickListener(this);
        luobo[5].setOnClickListener(this);
        luobo[6].setOnClickListener(this);
        luobo[7].setOnClickListener(this);
        luobo[8].setOnClickListener(this);
        luobo[9].setOnClickListener(this);
        luobo[10].setOnClickListener(this);
        luobo[11].setOnClickListener(this);
        luobo[12].setOnClickListener(this);
        luobo[13].setOnClickListener(this);
        luobo[14].setOnClickListener(this);
        luobo[15].setOnClickListener(this);
        //画萝卜
        drawluobo();

        //播放音乐
        mediaplayer();
    }

        //播放音乐
    private void mediaplayer(){
        mp= MediaPlayer.create(this, R.raw.sugar);
        mp.setLooping(true);
        try {
            //背景音乐准备
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //背景音乐播放
        mp.start();
    }

        //结束页面
        private void intent(){
            Intent intent;
            if (score<=0){
                intent=new Intent(MatchGame.this,MatchOverFail.class);
                mp.stop();
                timer.cancel();
                startActivity(intent);
            }else if (score<=3&&score>0){
                intent=new Intent(MatchGame.this,MatchOverOne.class);
                mp.stop();
                timer.cancel();
                startActivity(intent);
            }else if (score<=9){
                intent=new Intent(MatchGame.this,MatchOverTwo.class);
                mp.stop();
                timer.cancel();
                startActivity(intent);
            }else if (score<=18){
                intent=new Intent(MatchGame.this,MatchOverThree.class);
                mp.stop();
                timer.cancel();
                startActivity(intent);
            }else{
                intent=new Intent(MatchGame.this,MatchOverFail.class);
                mp.stop();
                timer.cancel();
                startActivity(intent);
            }
        }




    Random random=new Random();

    //存id
    int imageid[]={R.drawable.color1,R.drawable.color2,R.drawable.color3};

    //画萝卜,三种目标方式加方法
    private void drawluobo(){
        int s=random.nextInt(3);

        if(s==GREEN){
            selectColor.setImageResource(R.drawable.green_small);
            for (int i=0;i<luobo.length;i++) {
                int n = random.nextInt(3);
                luobo[i].setImageResource(imageid[n]);
                luobo[i].setTag(n);
            }
        }else if(s==ORANGE){
            selectColor.setImageResource(R.drawable.orange_small);
            for (int i=0;i<luobo.length;i++){
                int n=random.nextInt(3);
                luobo[i].setImageResource(imageid[n]);
                luobo[i].setTag(n);
            }
        }else if (s==PURPLE){
            selectColor.setImageResource(R.drawable.purple_small);
            for (int i=0;i<luobo.length;i++){
                int n=random.nextInt(3);
                luobo[i].setImageResource(imageid[n]);
                luobo[i].setTag(n);
            }
        }
    }


    //控制游戏时间
    TimerTask task = new TimerTask() {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {      // UI thread
                @Override
                public void run() {
                    recLen--;
                    if(recLen>=10){
                    gameTime.setText("00："+recLen);
                    }else{
                        gameTime.setText("00:0"+recLen);}
                    //如果时间结束，关闭页面
                    if(recLen <=0||score>18||score<0){
                        mp.stop();
                        intent();
                        System.gc();
                        System.exit(0);
                    }
                }
            });
        }
    };
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.leave_game:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("确认信息");
                builder.setMessage("你要退出游戏吗");
                builder.setCancelable(false);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.setCancelable(true);
                builder.show();
                break;
            case R.id.push_time:
                if (flag == 1) {
                    pushGame.setBackgroundResource(R.drawable.icon_stop_83px);
                    flag = 0;
                } else {
                    pushGame.setBackgroundResource(R.drawable.icon_play);
                    flag = 1;
                }
            case R.id.partt1:
                two_1 = 1;
                res1 = (int) luobo[0].getTag();
                change();
                break;
            case R.id.partt2:
                two_2 = 2;
                res2 = (int) luobo[1].getTag();
                change();
                break;
            case R.id.partt3:
                two_3 = 1;
                res3 = (int) luobo[2].getTag();
                change();
                break;
            case R.id.partt4:
                two_4 = 1;
                res4 = (int) luobo[3].getTag();
                change();
                break;
            case R.id.partt5:
                two_5 = 1;
                res5 = (int) luobo[4].getTag();
                change();
                break;
            case R.id.partt6:
                two_6 = 1;
                res6 = (int) luobo[5].getTag();
                change();
                break;
            case R.id.partt7:
                two_7 = 1;
                res7 = (int) luobo[6].getTag();
                change();
                break;
            case R.id.partt8:
                two_8 = 1;
                res8 = (int) luobo[7].getTag();
                change();
                break;
            case R.id.partt9:
                two_9 = 1;
                res9 = (int) luobo[8].getTag();
                change();
                break;
            case R.id.partt10:
                two_10 = 1;
                res10 = (int) luobo[9].getTag();
                change();
                break;
            case R.id.partt11:
                two_11 = 1;
                res11 = (int) luobo[10].getTag();
                change();
                break;
            case R.id.partt12:
                two_12 = 1;
                res12 = (int) luobo[11].getTag();
                change();
                break;
            case R.id.partt13:
                two_13 = 1;
                res13 = (int) luobo[12].getTag();
                change();
                break;
            case R.id.partt14:
                two_14 = 1;
                res14 = (int) luobo[13].getTag();
                change();
                break;
            case R.id.partt15:
                two_15 = 1;
                res15 = (int) luobo[14].getTag();
                change();
                break;
            case R.id.partt16:
                two_6 = 1;

                res16 = (int) luobo[15].getTag();
                change();
//                    if (res16==0){
//                        new AlertDialog.Builder(MatchGame.this)
//                                .setTitle("是红色的！")
//                                .setMessage("确实是红色的！")
//                                .show();
//                    }
//                    else if (res16==1){
//                        new AlertDialog.Builder(MatchGame.this)
//                                .setTitle("是黄色的！")
//                                .setMessage("确实是黄色的！")
//                                .show();
//                    }
//                    else if (res16==2){
//                        new AlertDialog.Builder(MatchGame.this)
//                                .setTitle("是蓝色的！")
//                                .setMessage("确实是蓝色的！")
//                                .show();
//                    }

                break;
            case R.id.point_button:
                if (point==1){
                    pointAnimation= AnimationUtils.loadAnimation(this,R.anim.size_change);
                    luobo[color].startAnimation(pointAnimation);
                    point=0;

                }else {
                    luobo[color].clearAnimation();
                    point = 1;
                }
                break;
            default:
                break;
        }
    }



    private void change(){


        if (two_1 == 1 && two_2==1 &&res1 == 0 && res2 == 1 || two_1 == 1 && two_2==1 &&res1 == 1 && res2 == 0) {
            two_1 = 0;
            two_2 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_2 == 1 && two_3 == 1&&res2 == 0 && res3 == 1 || two_2 == 1 && two_3 == 1&&res2 == 1 && res3 == 0) {
            two_2 = 0;
            two_3 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_3 == 1 && two_4 == 1&&res3 == 0 && res4 == 1 || two_3 == 1 && two_4 == 1&&res3 == 1 && res4 == 0) {
            two_3 = 0;
            two_4 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_5 == 1 && two_6 == 1&&res5 == 0 && res6 == 1 ||two_5 == 1 && two_6 == 1&& res5 == 1 && res6 == 0) {
            two_5 = 0;
            two_6 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_6 == 1 && two_7 == 1&&res6 == 0 && res7 == 1 || two_6== 1 && two_7 == 1&&res6 == 1 && res7 == 0) {
            two_6 = 0;
            two_7 = 0;
            score++;
            scoreText.setText(score + "/18");

            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_7 == 1 && two_8== 1&&res7 == 0 && res8 == 1 || two_7 == 1 && two_8== 1&&res7 == 1 && res8 == 0) {
            score++;
            two_7 = 0;
            two_8 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();

        } else if (two_9 == 1 && two_10 == 1&&res9 == 0 && res10 == 1 ||two_9 == 1 && two_10 == 1&& res9 == 1 && res10 == 0) {
            two_9 = 0;
            two_10 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
        } else if (two_10 ==1 && two_11 == 1&&res10 == 0 && res11 == 1 || two_10 == 1 && two_11 == 1&&res10 == 1 && res11 == 0) {
            two_10 = 0;
            two_11 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_11 == 1 && two_12 == 1&&res11 == 0 && res12 == 1 || two_11 == 1 && two_12 == 1&&res11 == 1 && res12 == 0) {
            two_11 = 0;
            two_12 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_13 == 1 && two_14 == 1&&res13 == 0 && res14 == 1 || two_13 == 1 && two_14 == 1&&res13 == 1 && res14 == 0) {
            two_13 = 0;
            two_14 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_14 == 1 && two_15 == 1&&res14 == 0 && res15 == 1 || two_14 == 1 && two_15 == 1&&res14 == 1 && res15 == 0) {
            two_14 = 0;
            two_15 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_15 == 1 && two_16 == 1&&res15 == 0 && res16 == 1 || two_15 == 1 && two_16 == 1&&res15 == 1 && res16 == 0) {
            two_15 = 0;
            two_16 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_1 == 1 && two_5 == 1&&res1 == 0 && res5 == 1 ||two_1 == 1 && two_5 == 1&& res1 == 1 && res5 == 0) {
            two_1 = 0;
            two_5 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_2 == 1 && two_6 == 1&&res2 == 0 && res6 == 1 ||two_2 == 1 && two_6 == 1&& res2 == 1 && res6 == 0) {
            two_2 = 0;
            two_6 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_3 == 1 && two_7 == 1&&res3 == 0 && res7 == 1 ||two_3== 1 && two_7 == 1&& res3 == 1 && res7 == 0) {
            two_3 = 0;
            two_7 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_4 == 1 && two_8 == 1&&res4 == 0 && res8 == 1 || two_4 == 1 && two_8 == 1&&res4 == 1 && res8 == 0) {
            two_4 = 0;
            two_8 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_5 == 1 && two_9 == 1&&res5 == 0 && res9 == 1 ||two_5 == 1 && two_9 == 1&& res5 == 1 && res9 == 0) {
            two_5 = 0;
            two_9 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_6 == 1 && two_10 == 1&&res6 == 0 && res10 == 1 ||two_6 == 1 && two_10 == 1&& res6 == 1 && res10 == 0) {
            two_6 = 0;
            two_10 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_7 == 1 && two_11 == 1&&res7 == 0 && res11 == 1 || two_7== 1 && two_11 == 1&&res7 == 1 && res11 == 0) {
            two_7 = 0;
            two_11 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_8 == 1 && two_12 == 1&&res8 == 0 && res12 == 1 ||two_8== 1 && two_12 == 1&& res8 == 1 && res12 == 0) {
            two_8 = 0;
            two_12 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_9 == 1 && two_13 == 1&&res9 == 0 && res13 == 1 ||two_9 == 1 && two_13== 1&& res9 == 1 && res13 == 0) {
            two_9 = 0;
            two_13 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_10== 1 && two_14 == 1&&res10 == 0 && res14 == 1 ||two_10== 1 && two_14== 1&& res10 == 1 && res14 == 0) {
            two_10 = 0;
            two_14 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_11 == 1 && two_15 == 1&&res11 == 0 && res15 == 1 || two_11 == 1 && two_15 == 1&&res11 == 1 && res15 == 0) {
            two_11 = 0;
            two_15 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }
        } else if (two_12 == 1 && two_16 == 1&&res12 == 0 && res16 == 1 || two_12 == 1 && two_16 == 1&&res12 == 1 && res16 == 0) {
            two_12 = 0;
            two_16 = 0;
            score++;
            scoreText.setText(score + "/18");
            progress = progressBar.getProgress();
            progress = progress + 10;
            progressBar.setProgress(progress);
            if (progress == 180 || score >= 18||recLen<=0) {
                intent();
            }
            drawluobo();
            if (recLen<=0){
                intent();
            }

        }

//        else {
//            score--;
//            scoreText.setText(score+"/18");
//
//            progress = progressBar.getProgress();
//            progress = progress -10;
//            progressBar.setProgress(progress);
//            if (progress==0||score<0) {
//                intent();
//            }
//            drawluobo();
//              }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        mp.release();
    }
}
